const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "新手机号绑定", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      goUrl: ''
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    openid: '',
    phone: "",
    verify: "",
    codeDjs: 0,
    showpop: false,
    sendTel: '',
    sendSjc: 0,
    sendCode: '',
    shareTitle: '',
    shareImg: '',
    oldTel: '',
    flag: 0
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var isOldTelCheck = options.isOldTelCheck;
    if (isOldTelCheck != '1') {
      app.showTip('请先完成现有账号绑定的手机号码验证后再绑定新的手机号');
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        });
      }, 2000);
      return;
    }

    var openid = wx.getStorageSync('ly24_openid');
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getChangeTelNewTelPageInfo(res.data.openid);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getChangeTelNewTelPageInfo(openid);
    }
    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getChangeTelNewTelPageInfo(openid) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getChangeTelNewTelPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          that.setData({
            oldTel: res.data.oldTel
          });
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  //点击完成绑定
  btn() {
    var that = this;
    if (that.data.phone.trim() === '') {
      app.showTip("请输入电话");
      return;
    } else if (that.data.phone.trim() == that.data.oldTel) {
      app.showTip("绑定的新手机号与现在账号绑定的手机不能一致哦");
      return;
    } else if (that.data.verify.trim() === '') {
      app.showTip("请输入验证码");
      return;
    } else if (that.data.verify.trim().length != 6 || isNaN(that.data.verify.trim())) {
      app.showTip("请输入6位数字的验证码");
      return;
    } else if (that.data.sendTel != that.data.phone.trim() && that.data.sendTel != '') {
      app.showTip("抱歉,您提交的电话号码和发送短信的电话号码不一致");
      return;
    } else if (that.data.sendCode != that.data.verify.trim() && that.data.sendCode != '') {
      app.showTip("验证码错误");
      return;
    } else {
      if (that.data.flag==0) {
        that.setData({
          flag: 1
        });
        //后台验证并修改电话
        wx.showLoading({
          title: '信息提交中...',
          mask: true
        });
        wx.request({
          url: that.data.baseUrl + 'editUserTel.php',
          method: 'POST',
          data: {
            openid: that.data.openid,
            tel: that.data.phone.trim(),
            code: that.data.verify.trim(),
            sendCode: that.data.sendCode,
            sendTel: that.data.sendTel,
            sendSjc: that.data.sendSjc
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            wx.hideLoading();
            app.showTip(res.data.msg);
            if (res.data.status == 1) {
              //回跳个人中心页
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/my/index'
                });
              }, 2000);
            } else if (res.data.status == 100) {
              //现有手机号已绑定账户提示
              that.setData({
                showpop: true
              });
              that.setData({
                flag: 0
              });
            } else {
              that.setData({
                flag: 0
              });
            }
          },
          fail: function (res) {
            wx.hideLoading();
            app.showTip('服务器繁忙,请稍候重试');
            that.setData({
              flag: 0
            });
          }
        });
      }
    }
  },
  verifybtn() {
    const that = this;
    //判断是否重复点击
    if (that.data.codeDjs > 0 && that.data.codeDjs < 60) {
      return;
    }
    if (this.data.phone.trim() === '') {
      app.showTip("请输入电话")
    } else if (this.data.phone.trim().length != 11) {
      app.showTip("请输入正确的电话")
    } else if (that.data.phone.trim() == that.data.oldTel) {
      app.showTip("绑定的新手机号与现在账号绑定的手机不能一致哦");
      return;
    } else {
      //后台请求发送验证码
      wx.showLoading({
        title: '验证码发送中...'
      });
      wx.request({
        url: that.data.baseUrl + 'sms/editTelSendCode.php',
        method: 'POST',
        data: {
          openid: that.data.openid,
          tel: that.data.phone.trim(),
          flag: 'new'
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          wx.hideLoading();
          app.showTip(res.data.msg);
          if (res.data.status == 1) {
            that.setData({
              sendTel: res.data.sendTel,
              sendSjc: res.data.sendSjc,
              sendCode: res.data.sendCode
            });
            //处理时间
            that.setData({
              codeDjs: 60
            })
            let interval = setInterval(function () {
              if (that.data.codeDjs > 0) {
                that.data.codeDjs--;
                console.log(that.data.codeDjs)
                that.setData({
                  codeDjs: that.data.codeDjs
                })
              } else {
                clearInterval(interval);
              }
            }, 1000);
          }
        },
        fail: function (res) {
          wx.hideLoading();
          app.showTip('服务器繁忙,请稍候重试');
        }
      });
    }
  },
  knowBtn() {
    this.setData({
      showpop: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})